require.config({
    baseUrl: baseUrl
});

define(function(require) {
    var $ = require('jquery');
    window.$ = window.jQuery = $;
    var dialog = require('bDialog/dialog');
    window.dialog=dialog;
    require('scrollpage/jquery-ias-min-v2.0');
    var c = require('../common');
    require('starScore');//星星打分

    $(function() {
        c.init();

        /**
         滚动加载分页
         **/
        if(!empty($('.item').html())){
            var ias = $('.container').ias({
                container:  ".contents",
                item:       ".item",
                pagination: "#page",
                next:       ".next",
            });

            ias.extension(new IASSpinnerExtension());
            ias.extension(new IASTriggerExtension({offset: 10}));
            ias.extension(new IASNoneLeftExtension({
                text: '<div style="padding:15px">没有了.</div>'
            }));
            ias.extension(new IASPagingExtension());
            ias.on('pageChange', function(pageNum, scrollOffset, url) {
                ias.on('loaded', function(data,items) {
                    $('#page').html($(data).find('#page').html());
                });
            });
        }

        //编辑收货地址
        $('.edit-address').click(function(event) {
            var t=$(this);
            var type=t.data('type');
            if(type=='edit'){
                $('.adrslist li').find('.op').show();
                t.html('完成');
                t.data('type','complete');
                if($('.radiobox').length){
                    $('.radiobox').hide();
                }
            }else{
                $('.adrslist li').find('.op').hide();
                t.html('编辑');
                t.data('type','edit');
                if($('.radiobox').length){
                    $('.radiobox').show();
                }
            }
        });

        //删除
        $('.adrslist').delegate('.op .delete', 'click', function(event) {
            var t = $(this);
            var id = t.data('id');
            if(empty(id)){
                c.error('请选择操作项!',2000);
                return false;
            }
            dialog({title:'温馨提示',height:100,width:500,content:'<div class="tc">是否确定删除该地址？</div>',ok:function(){
                $.get("/Address/del?id="+id,function(data){
                    if(data.status == 1){
                        c.success(data.info,1000,function(){
                            t.parents('li').remove();
                        });
                    }else{
                        c.error(data.info,2000);
                        return false;
                    }
                })
            },
                cancel:function(){},
                okVal:'确定',cancelVal:'取消'});
        });

        //添加\编辑收货地址
        $('#saveAddress').click(function(){
            var contact = $.trim($('input[name="contact"]').val());
            var mobile = $.trim($('input[name="mobile"]').val());
            var locations = $.trim($('input[name="location"]').val());
            var maddress = $.trim($('.address .ads').html());
            var saddress = $.trim($('textarea[name="saddress"]').val());
            var id = $('input[name="id"]').val();
            var sid = $('input[name="sid"]').val();

            if(empty(contact)){
                c.error('请输入联系人',2000);
                return false;
            }

            if(empty(mobile)){
                c.error('请输入联系方式',2000);
                return false;
            }

            var type = $('.address').data('type');
            var forms = {};
            forms['id'] = id;
            forms['contact'] = contact;
            forms['mobile'] = mobile;
            forms['location'] = locations;
            forms['main_address'] = maddress;
            forms['sub_address'] = saddress;
            $.post("/Address/update",forms,function(data){
               if(data.status == 1){
                   c.success(data.info,1000);
                   setTimeout(function(){
                       if(type == 2){
                           location.href = "/Info/info";
                       }else{
                           if(sid){
                               location.href = "/Product/cart?id="+sid+"&aid="+data.data;
                           }else{
                               location.href = "/Address/index";
                           }
                       }
                   },1000);
                   return true;
               }else{
                   c.error(data.info,2000);
                   return false;
               }
            });
        });

        //跳转地图
        $('.address').click(function(){
            var type = $(this).data('type');
            var contact = $.trim($('input[name="contact"]').val());
            var mobile = $.trim($('input[name="mobile"]').val());
            var sid = $('input[name="sid"]').val();
            location.href = "/Address/map?contact="+contact+"&mobile="+mobile+"&type="+type+"&sid="+sid;
        });

        //星星打分
        $("#star").scoreFun({
            fen_d:70,//每一个a的宽度
            ScoreGrade:5//a的个数 10或者
        });

        //评论
        $('#comment').click(function(){
            var orderId = $('input[name="order_id"]').val();
            var content = $.trim($('textarea[name="content"]').val());
            var score = $('.fenshu').html();
            if(empty(orderId)){
                c.error('请选择操作订单!',2000);
                return false;
            }

            if(empty(score) || score == 0){
                c.error('请先打分!',2000);
                return false;
            }
            if(empty(content)){
                c.error('请输入评论内容!',2000);
                return false;
            }

            var forms = {};
            forms['id'] = orderId;
            forms['content'] = content;
            forms['score'] = score;
            $.post(URL+"/comment",forms,function(data){
               if(data.status == 1){
                   c.success(data.info,1000,function(){
                       location.href = URL+"/detail?id="+orderId;
                   });
                   return true;
               }else{
                   c.error(data.info,2000);
                   return false;
               }
            });
        });

        //地址选择
        $('input[name="address"]').click(function(){
            var selVal = $(this).val();
            if(empty(selVal)) return false;
            var sid = $('input[name="shop_id"]').val();
            location.href = URL+"/cart?id="+sid+"&aid="+selVal;
        });

        //支付
        $('.pay').click(function(){
            var id = $(this).data('id');
            if(empty(id)){
                c.error('请选择支付订单!',2000);
                 return false;
            }
            location.href = encodeURI(SITEURL + '/WxPay?orderid='+id);
        });

        //确定完成
        $('.queComplete').click(function(){
            var id = $(this).data('id');
            if(empty(id)){
                c.error('请选择操作项!',2000);
                return false;
            }
            $.post(URL+"/complete",{'id':id},function(data){
                if(data.status == 1){
                    c.success(data.info,1000,function(){
                        location.reload();
                    });
                    return true;
                }else{
                    c.error(data.info,2000);
                    return false;
                }
            })
        });
    });
});